var organizationItem_table;
$(function(){
	organizationItem_table = $('#organizationItem_table_id');
	organizationItem_table.bootstrapTable({
		url : "/getOrganizationItemPage",
		striped : true, // 是否显示行间隔色

		search : true,

		//导出
		showExport : true,
        exportOptions:{
            fileName: '商品统计报表',  //文件名称设置
            worksheetName: 'sheet1',  //表格工作区名称
            tableName: '商品统计报表',
            excelstyles: ['background-color', 'color', 'font-size', 'font-weight'],
        },

		// 分页相关
		sidePagination : 'server',
		pagination : true, // 是否显示分页（*）
		pageNumber : 1, // 初始化加载第一页，默认第一页,并记录
		pageSize : 10, // 默认显示几条数据 
		pageList : [ 10, 50, 100, 1000], // 用于设置当前页面可以显示的数据条数
		queryParams : function(params) {
			var nparams = {
				'search' : params.search,
				'pageNumber' : (params.offset/params.limit)+1,
				'pageSize' : params.limit
			};
			return nparams;
		},
		showRefresh : true, // 是否显示刷新按钮
		uniqueId : 'id',
		columns : [ {
			checkbox : true,
			visible : true
		// 是否显示复选框 
		}, {
			field : 'id',
			title : '商品编号',
			width : "10%"
		}, {
			field : 'name',
			title : '商品名称',
			width : "20%"
		}, {
			field : 'price',
			title : '价格',
			width : "10%"
		}, {
			field : 'image',
			title : '图片',
			width : "10%",
			formatter:function(value,row, index){
				if(value == null || value.length == 0)
					return "无";
				else
					return '<a href="'+value+'" target="_blank"><button class="btn btn-primary">查看</button></a>';
	    	}
		}, {
			field : 'organization.name',
			title : '所属机构',
			width : "50%"
		}]
	});

	var add_organizationItem_btn = $("#add_organizationItem_btn_id");
	add_organizationItem_btn.click(function() {
		$('#add_organizationItem_Modal').modal("show");
	});
	var up_organizationItem_btn = $("#up_organizationItem_btn_id");
	up_organizationItem_btn.click(function() {
		var selectRows = organizationItem_table.bootstrapTable("getSelections");
		if (selectRows.length != 1) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '请选择一行记录'
			});
			return;
		}
		$('#up_organizationItem_id').val(selectRows[0].id);
		$('#up_organizationItem_name').val(selectRows[0].name);
		$('#up_organizationItem_image').val('');
		$('#up_organizationItem_price').val(selectRows[0].price);

		$('#up_organizationItem_Modal').modal("show");
	});
	var add_organizationItem_submit_btn = $("#add_organizationItem_submit_btn_id");
	add_organizationItem_submit_btn.click(function() {
		$('#add_organizationItem_Modal').modal("hide");
		$('#waiting_Modal').modal("show");
		$("#add_organizationItem_form_id").ajaxSubmit({
			type : 'POST',
			url : '/addOrganizationItem',
			success : function(data) {
				var type;
				if (data.status == 0) {
					organizationItem_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				$('#waiting_Modal').modal("hide");
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});
	});
	var up_organizationItem_submit_btn = $("#up_organizationItem_submit_btn_id");
	up_organizationItem_submit_btn.click(function() {
		$('#up_organizationItem_Modal').modal("hide");
		$('#waiting_Modal').modal("show");
		$("#up_organizationItem_form_id").ajaxSubmit({
			type : 'POST',
			url : '/upOrganizationItem',
			success : function(data) {
				var type;
				if (data.status == 0) {
					organizationItem_table.bootstrapTable("refresh");
					type = BootstrapDialog.TYPE_SUCCESS;
				} else if (data == 1) {
					type = BootstrapDialog.TYPE_WARNING;
				} else {
					type = BootstrapDialog.TYPE_DANGER;
				}
				$('#waiting_Modal').modal("hide");
				BootstrapDialog.show({
					type : type,
					title : "执行结果",
					buttons : [ {
						label : '关闭',
						action : function(dialogItself) {
							dialogItself.close();
						}
					} ],
					message : data.msg
				});
			}
		});

	});
	var del_organizationItem_btn = $("#del_organizationItem_btn_id");
	del_organizationItem_btn.click(function() {
		var selectRows = organizationItem_table.bootstrapTable("getSelections");
		if (selectRows.length == 0) {
			BootstrapDialog.show({
				type : BootstrapDialog.TYPE_WARNING,
				title : "错误",
				buttons : [ {
					label : '关闭',
					action : function(dialogItself) {
						dialogItself.close();
					}
				} ],
				message : '至少选择一行记录'
			});
			return;
		} else {
			var ids = [];
			$.each(selectRows, function(i, row) {
				ids.push(row.id);
			});

			$.ajax({
				type : 'POST',
				url : '/delOrganizationItem',
				data : {
					"ids" : ids.join(",")
				},
				success : function(data) {
					var type;
					if (data.status == 0) {
						organizationItem_table.bootstrapTable("refresh");
						type = BootstrapDialog.TYPE_SUCCESS;
					} else if (data == 1) {
						type = BootstrapDialog.TYPE_WARNING;
					} else {
						type = BootstrapDialog.TYPE_DANGER;
					}
					BootstrapDialog.show({
						type : type,
						title : "执行结果",
						buttons : [ {
							label : '关闭',
							action : function(dialogItself) {
								dialogItself.close();
							}
						} ],
						message : data.msg
					});
				}
			});
		}
	});

});
